package com.longdaji.decoration.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

import com.longdaji.decoration.R;
import com.longdaji.decoration.data.bean.LogisticsBean;
import com.longdaji.decoration.ui.pay.LogisticsPatternAdapter;
import com.longdaji.decoration.utils.ViewUtils;

import java.util.List;

public class LogisticsDialog extends Dialog {

    private Callback mCallBack;
    private Context mContext;
    private Button mBtnSubmit;
    private RecyclerView mRvLogistics;
    private List<LogisticsBean> mLogisticsList;
    private LogisticsPatternAdapter mAdapter;

    public LogisticsDialog(Context context, List<LogisticsBean> list, Callback callback) {
        //重点实现R.style.DialogStyle 动画效果
        this(context, R.style.CommonBottomDialogStyle);
        this.mContext = context;
        this.mCallBack = callback;
        this.mLogisticsList = list;
    }

    public LogisticsDialog(Context context, int themeResId) {
        super(context, themeResId);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_logistics);
        // 拿到Dialog的Window, 修改Window的属性
        Window window = getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        // 获取Window的LayoutParams
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
        attributes.height = ViewUtils.getScreenHeightPixels(mContext) * 1 / 2;
        attributes.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        // 一定要重新设置, 才能生效
        window.setAttributes(attributes);
        initView();
    }

    private void initView() {
        mRvLogistics = findViewById(R.id.recycler_view_logistics);
        mBtnSubmit = findViewById(R.id.btn_submit);
        if (mLogisticsList != null && mLogisticsList.size() > 0) {
            mLogisticsList.get(0).setSelect(true);
        }
        mAdapter = new LogisticsPatternAdapter(mLogisticsList);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        mRvLogistics.setLayoutManager(layoutManager);
        mRvLogistics.setAdapter(mAdapter);
        mBtnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCallBack.onSubmit(mAdapter.getSelectItem());
                dismiss();
            }
        });
    }

    public interface Callback {
        void onSubmit(LogisticsBean logisticsBean);

    }
}
